﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Timers;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace TaskPublishService
{
    partial class TimerPublishService : ServiceBase
    {
        public TimerPublishService()
        {
            InitializeComponent();
            //Main m = new Main();
            //m.ShowDialog();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
            //Main m = new Main();
            //m.ShowDialog();
            System.Timers.Timer t = new System.Timers.Timer(3000);
            t.Elapsed += new ElapsedEventHandler(TimeElapse);
            t.AutoReset=true;
            t.Enabled = true;
        }

        public void TimeElapse(object source, ElapsedEventArgs e)
        {

            StreamWriter sw = new StreamWriter(@".\log\" + DateTime.Today.ToString("yyyy-MM-dd") + "_"  + ".txt");//+ dr["f_telno"].ToString()
            sw.AutoFlush = true;
            Console.SetOut(sw);


            //MessageBox.Show("ok");
            File.AppendAllText("d:/a.txt", "过了3秒" + DateTime.Now.ToString() + System.Environment.NewLine, Encoding.Default);

            SqlConnection sql = new SqlConnection();
            try
            {
                //if (this.textBox1.Text.Trim() != "")
                //    sql.ConnectionString = this.textBox1.Text.Trim();
                sql.ConnectionString = ConfigurationManager.AppSettings["DBHost"].ToString();
                sql.Open();
            }
            catch (Exception ex)
            {
                File.AppendAllText("d:/a.txt", "出错了" + ex.Message + System.Environment.NewLine, Encoding.Default);
                //MessageBox.Show(ex.Message);
            }
            finally
            {
                sql.Close();
            }
        }

        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
        }

    }
}
